<?php
/**
 * Get tabs data
 * @author vicky
 */
function get_tabs_data() {
  $parent = taxonomy_get_parents(arg(2));
  if (!empty($parent)) {
    foreach ($parent as $v) {
      $tabs = taxonomy_get_children($v->tid);
    }
  }
  else {
    $tabs = taxonomy_get_children(arg(2));
  }

  return $tabs;
}

/**
 * Get tabs html
 * @author vicky
 */
function get_tabs_html(&$html) {
  $data['active'] = $data['url'] = $data['name'] = '';
  $tabs = get_tabs_data();
  foreach ((array) $tabs as $k => $v) {
    $data['active'][$k] = arg(2) == $v->tid ? ' class="active"' : '';
    $data['url'][$k] = url(drupal_get_path_alias('taxonomy/term/' . $v->tid));
    $data['name'][$k] = $v->name;
  }
  $html .= theme('kavo_product_list_tabs', $data);
}

/**
 * Get nodes html
 * @author vicky
 */
function get_nodes_html(&$html) {
  $results = get_views_results('product_list');
  $total_rows = $results->total_rows;
  if (isset($results->result) && !empty($results->result)) {
    $result = $results->result;
    $html .= '<div class="product-warp">';
    foreach ((array) $result as $v) {
      $node = get_online_node($v->nid);
      if (!$node) {
        continue;
      }
      $data['title'] = $node->title;
      $data['url'] = url(drupal_get_path_alias('node/' . $node->nid));
      $data['image'] = image_style_url("product_detail", get_field_value("field_product_images", $node));
      $data['series'] = get_field_value("field_product_series", $node);
      $html .= theme('kavo_product_list_nodes', $data);
    }
    $html .= '</div>';
    $html .= get_page_html($total_rows, 12);
  }
}

/**
 * Get Kerr修复 html
 * @author vicky
 */
function get_kerr_html(&$html) {
  $data['url'] = $data['name'] = $data['node_title'] = '';
  $results = get_views_results('product_list_kerr_taxonomy', 'page', '', ['product_parent_taxonomy' => arg(2)]);
  $total_rows = $results->total_rows;
  if (isset($results->result) && !empty($results->result)) {
    $terms = $results->result;
    $html .= '<div class="product-warp category">';
    foreach ($terms as $t) {
      $v = taxonomy_term_load($t->tid);
      $data['url'] = image_style_url('pro_tax', get_field_value('field_pro_tax_image', $v));
      $data['name'] = $v->name;
      $nodes = get_views_results('product_list_kerr', 'page', '', ['field_product_taxonomy_tid' => $v->tid]);
      foreach ((array) $nodes->result as $node) {
        $node = get_online_node($node->nid);
        $data['node_title'][$node->nid] = $node->title;
      }
      $html .= theme('kavo_product_list_kerr', $data);
      unset($data['node_title']);
    }
    $html .= '</div>';
    $html .= get_page_html($total_rows, 9);
  }
}

/**
 * 输出分页
 */
function get_page_html($count, $page_size = 10) {
  var_dump(pager_default_initialize($count, $page_size));exit;
  return theme('pager');
}

/**
 * Get product list html.
 * @author vicky
 */
function theme_kavo_product_list() {
  drupal_add_css(drupal_get_path('theme', 'kavo') . '/css/product.css', array('group' => CSS_THEME));
  drupal_add_js(drupal_get_path('theme', 'kavo') . '/js/product.js');
  get_tabs_html($html);
  $parent = taxonomy_get_parents(arg(2));
  if (key($parent) == 2 || key($parent) == 4) {
    get_kerr_html($html);
  }
  else {
    get_nodes_html($html);
  }

  return $html;
}

/**
 * Product list display in the menu ajax.
 * @author vicky
 */
function ajax_is_menu($nid, $value) {
  $node = get_online_node($nid, TRUE);
  if (!$node) {
    echo json_encode(['status' => '当前内容无上线版本，请上线后再执行此操作。']);
    die;
  }
  $tid = get_field_value('field_product_taxonomy', $node);
  $nids = variable_get('product_list_menu_count_' . $tid);
  if (empty($value)) {
    unset($nids[$nid]);
    variable_set('product_list_menu_count_' . $tid, $nids);
  }
  else {
    if (!isset($nids[$nid])) {
      if (count($nids) >= 6) {
        echo json_encode(['status' => '菜单中最多可显示6项。']);
        exit();
      }
      else {
        $nids[$nid] = 1;
        variable_set('product_list_menu_count_' . $tid, $nids);
      }
    }
  }
  $node->field_display_in_menu[LANGUAGE_NONE][0]['value'] = $value;
  node_save($node);
  echo json_encode(['status' => 'success']);
  exit();
}

/**
 * Admin, edit product node workflow change to draft.
 * @author vicky
 */
function workflow_change_to_draft(&$form, &$form_state) {
  op_change_workflow($form_state['nid'], DRAFT);
}

/**
 * Submit a form to change the workflow status to pending apprvoe
 * @author vicky <xiaolin.li@pactera.com>
 * @return [type] [description]
 */
function change_to_pending_approve(&$form, &$form_state) {
  $status = is_admin() ? ONLINE : PENDING_APPROVE;
  op_change_workflow($form_state['nid'], $status, TRUE);
}